/* cl.ext.countUp 0.1*/
(
    function (factory) {
        if (typeof define === 'function' && define.amd) {
            define(['jquery',"CountUp"], function($,countUp) {
                factory($,countUp);
            });
        } else if (typeof module === 'object' && module.exports) {
            var countUpJs = require('CountUp');
            module.exports = function( root, jQuery ) {
                if ( jQuery === undefined ) {
                    if ( typeof window !== 'undefined' ) {
                        jQuery = require('jquery');
                    }
                    else {
                        jQuery = require('jquery')(root);
                    }
                }
                factory(jQuery,countUpJs);
            };
        } else {
            factory(jQuery,CountUp);
        }
    }(
    function ($,CountUp){
        var defaultExtInfo={start:0,end:0,decimals:0,time:2};
        var defaults={
            useEasing : true, useGrouping : true, separator : ',', decimal : '.', prefix : '', suffix : '' ,
            infoExt:defaultExtInfo
        };
        var $modalCommon={
            getDefaults :function () {
                return defaults
            },
            getOptions : function (options) {
                options=$.ExtCommon.isCheckEmpty(options)?{}:options;
                return $.ExtCommon.initMergeJson(this.getDefaults(),options);
            }
        };
        var CountUpExtModal = function(element,options) {
            this.element = $(element);
            this.optionBase=$modalCommon.getOptions(options);
            var optionCount={};
            var id=this.element.attr("id");
            if($.ExtCommon.isCheckEmpty(id)){
                id=this.element.prop('tagName').toLowerCase()+"_"+$.ExtCommon.getRandom(7);
                this.element.attr("id",id);
            }
            optionCount.id=id;
            if(this.element.hasClass("num-int")){optionCount.decimals=0}
            if(this.element.hasClass("num-float2")){optionCount.decimals=2}
            this.optionBase.infoExt=$.ExtCommon.initMergeJson(this.optionBase.infoExt,optionCount);
            this._init()
        };
        CountUpExtModal.prototype = {
            _init:function () {
                var info=this.optionBase.infoExt;
                this.objCountUp=new CountUp(info.id, info.start, info.end, info.decimals, info.time, this.optionBase);
            },
            start:function (start,end) {
                this.optionBase.infoExt.start=start||0;
                this.optionBase.infoExt.end=end||0;
                this._init();
                this.objCountUp.start();
            },
            update:function (end) {
                this.optionBase.infoExt.end=end||0;
                this.objCountUp.update(end);
            },
            refresh:function (end) {
                
            }
        };
        $.fn.ExtCountUp = function(options) {
            var dataFun;
            this.each(function() {
                var el = $(this);
                var tempData=el.data('ExtCountUp');
                if (tempData) {
                    if (typeof  options == "string") {
                        if (tempData[options]) {
                            dataFun =tempData[options]()
                        } else {
                            dataFun = tempData
                        }
                    } else {
                        dataFun = tempData
                    }
                } else {
                    dataFun = new CountUpExtModal(el, options);
                    el.data('ExtCountUp', dataFun);
                }
            });
            return dataFun;
        };
        return CountUpExtModal;
    })
);
